package com.kabuda.zhugehot200_review;

/**
 * @author kabuda
 * @date 2025-03-29 15:46
 * @description
 */
public class 排列硬币1 {
    public static void main(String[] args) {
        System.out.println(arrangeCoins(5));
    }

    /**
     * @param n
     * @author kabuda
     * @date 2025/3/29 15:47
     * @description 二分查找
     */
    public static int arrangeCoins(int n) {
        int left = 1, right = n;
        while (left <= right){
            int mid = left + (right - left) / 2;
            if ((long) mid * (mid + 1) / 2 <= n && (long) (mid + 1) * (mid + 2) / 2 > n){
                return mid;
            }else if ((long) mid * (mid + 1) / 2 > n){
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return 0;
    }

}
